home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jed096_1.zip / SLANG / SRC / SLMATH.C < prev    next >
C/C++ Source or Header  |  1994-04-26  |  5KB  |  169 lines

  1. /* sin, cos, etc, for S-Lang */
  2. /* 
  3.  * Copyright (c) 1992, 1994 John E. Davis 
  4.  * All rights reserved.
  5.  *
  6.  * Permission is hereby granted, without written agreement and without
  7.  * license or royalty fees, to use, copy, and distribute this
  8.  * software and its documentation for any purpose, provided that the
  9.  * above copyright notice and the following two paragraphs appear in
  10.  * all copies of this software.
  11.  *
  12.  * IN NO EVENT SHALL JOHN E. DAVIS BE LIABLE TO ANY PARTY FOR DIRECT,
  13.  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
  14.  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF JOHN E. DAVIS
  15.  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * JOHN E. DAVIS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
  18.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  19.  * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
  20.  * BASIS, AND JOHN E. DAVIS HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
  21.  * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  22.  */
  23.  
  24.  
  25. #include <math.h>
  26.  
  27. #ifndef FLOAT_TYPE 
  28. #define FLOAT_TYPE 5
  29. #endif
  30.  
  31. #include "slang.h"
  32. #include "_slang.h"
  33.  
  34.  
  35. #if defined(_MSC_VER) && defined(_MT)
  36. static FLOAT dmath1(double (_pascal *f) (double))
  37. #else
  38. static FLOAT dmath1(double (*f) (double))
  39. #endif
  40. {
  41.    FLOAT x; 
  42.    int dum1, dum2;
  43.    if (SLang_pop_float(&x, &dum1, &dum2)) return(0.0);
  44.    
  45.    return (FLOAT) (*f)((double) x);
  46. }
  47.  
  48. #if defined(_MSC_VER) && defined(_MT)
  49. static FLOAT dmath2(double (_pascal *f) (double, double))
  50. #else
  51. static FLOAT dmath2(double (*f) (double, double))
  52. #endif
  53. {
  54.    FLOAT x, y; 
  55.    int dum1, dum2;
  56.    if (SLang_pop_float(&y, &dum1, &dum2)
  57.        || SLang_pop_float(&x, &dum1, &dum2)) return (0.0);
  58.  
  59.    return (FLOAT) (*f)((double) x, (double) y);
  60. }
  61.  
  62.  
  63.  
  64. FLOAT math_cos    () { return (FLOAT) dmath1(cos); }
  65. FLOAT math_sin    () { return (FLOAT) dmath1(sin); }
  66. FLOAT math_tan    () { return (FLOAT) dmath1(tan); }
  67. FLOAT math_atan    () { return (FLOAT) dmath1(atan); }
  68. FLOAT math_acos    () { return (FLOAT) dmath1(acos); }
  69. FLOAT math_asin    () { return (FLOAT) dmath1(asin); }
  70. FLOAT math_exp    () { return (FLOAT) dmath1(exp); }
  71. FLOAT math_log    () { return (FLOAT) dmath1(log); }
  72. FLOAT math_sqrt    () { return (FLOAT) dmath1(sqrt); }
  73. FLOAT math_log10() { return (FLOAT) dmath1(log10); }
  74. FLOAT math_pow()   { return (FLOAT) dmath2(pow); }
  75.  
  76. /* usage here is a1 a2 ... an n x ==> a1x^n + a2 x ^(n - 1) + ... + an */
  77. FLOAT math_poly()
  78. {
  79.    int n;
  80.    int dum1, dum2;
  81.    double xn = 1.0, sum = 0.0;
  82.    FLOAT an, x;
  83.    
  84.    if ((SLang_pop_float(&x, &dum1, &dum2))
  85.        || (SLang_pop_integer(&n))) return(0.0);
  86.    
  87.    while (n-- > 0)
  88.      {
  89.     if (SLang_pop_float(&an, &dum1, &dum2)) break;
  90.     (void) dum1; (void) dum2;
  91.     sum += an * xn;
  92.     xn = xn * x;
  93.      }
  94.    return((FLOAT) sum);
  95. }
  96.  
  97. #ifdef USE_DOUBLE
  98. static FLOAT Const_E =  2.718281828459045;
  99. static FLOAT Const_Pi = 3.141592653589793;
  100. #else
  101. static FLOAT Const_E =  2.7182818;
  102. static FLOAT Const_Pi = 3.1415926;
  103. #endif
  104.  
  105. static FLOAT slmath_do_float(void)
  106. {
  107.    FLOAT f = 0.0;
  108.    unsigned char type;
  109.    SLang_Object_Type obj;
  110.    
  111.  
  112.    if (SLang_pop(&obj)) return(f);
  113.  
  114.    type = obj.type >> 8;
  115.    if (type == INT_TYPE)
  116.      {
  117.     f = (FLOAT) obj.v.i_val;
  118.      }
  119.    else if (type == FLOAT_TYPE)
  120.      {
  121.     f = obj.v.f_val;
  122.      }
  123.    else if (type == STRING_TYPE)
  124.      {
  125.     /* Should check for parse error here but later. */
  126.     f = atof(obj.v.s_val);
  127.     if ((obj.type & 0xFF) == LANG_DATA) FREE(obj.v.s_val);
  128.      }
  129.    else SLang_Error = TYPE_MISMATCH;
  130.    return f;
  131. }
  132.  
  133. static SLang_Name_Type slmath_table[] =
  134. {
  135.    MAKE_INTRINSIC(".polynom", math_poly, FLOAT_TYPE, 0),
  136.    /* Usage:
  137.        a b .. c n x polynom  =y
  138.       This computes:
  139.        ax^n + bx^(n - 1) + ... c = y  */
  140.    MAKE_INTRINSIC(".sin", math_sin, FLOAT_TYPE, 0),
  141.    MAKE_INTRINSIC(".cos", math_cos, FLOAT_TYPE, 0),
  142.    MAKE_INTRINSIC(".tan", math_tan, FLOAT_TYPE, 0),
  143.    MAKE_INTRINSIC(".atan", math_atan, FLOAT_TYPE, 0),
  144.    MAKE_INTRINSIC(".acos", math_acos, FLOAT_TYPE, 0),
  145.    MAKE_INTRINSIC(".asin", math_asin, FLOAT_TYPE, 0),
  146.    MAKE_INTRINSIC(".exp", math_exp, FLOAT_TYPE, 0),
  147.    MAKE_INTRINSIC(".log", math_log, FLOAT_TYPE, 0),
  148.    MAKE_INTRINSIC(".sqrt", math_sqrt, FLOAT_TYPE, 0),
  149.    MAKE_INTRINSIC(".log10", math_log10, FLOAT_TYPE, 0),
  150.    MAKE_INTRINSIC(".pow", math_pow, FLOAT_TYPE, 0),
  151.    MAKE_VARIABLE(".E", &Const_E, FLOAT_TYPE, 1),
  152.    MAKE_VARIABLE(".PI", &Const_Pi, FLOAT_TYPE, 1),
  153.    MAKE_INTRINSIC(".float",  slmath_do_float, FLOAT_TYPE, 0),
  154.    /* Convert from integer or string representation to floating point.  
  155.       For example, "12.34" float returns 12.34 to stack.
  156.       as another example, consider:
  157.       1 2 /   ==>  0  since 1 and 2 are integers
  158.       1 2 float / ==> 0.5 since float converts 2 to 2.0 and floating point 
  159.       division is used.
  160.       */
  161.    SLANG_END_TABLE
  162. };
  163.  
  164. int init_SLmath()
  165. {
  166.    return SLang_add_table(slmath_table, "_Math");
  167. }
  168.  
  169.